<template>
  <div class="roles_content users_content">
    <div class="card-container">
    <div class="header">
      <el-form
        class="demo-form-inline"
        size="mini"
        label-position="right"
        ref="queryRoleRef"
        :model="formOptions"
        label-width="100px"
      >
        <div class="row">
          <div class="col">
            <el-form-item label="角色名称" prop="nameZh">
              <el-input
                v-model="formOptions.nameZh"
                placeholder="请输入角色名称"
                clearable
              ></el-input>
            </el-form-item>
          </div>
          <div
          style="width:250px"
          >
              <el-button class="header-btn" type="primary" size="mini" @click="resetForm('queryRoleRef')">
                <img src="../../../assets/img/globalTable/icon2-reset.png" alt="">
                重置</el-button
              >
              <el-button class="header-btn" type="primary" size="mini" @click="queryRolesclick"
                >
                <img src="../../../assets/img/globalTable/icon1-search.png" alt="">
                查询</el-button
              >
              <el-button class="header-btn" type="primary" size="mini" @click="addClick">
                <img src="../../../assets/img/globalTable/icon3-add.png" alt="">
                新增</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <el-card >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        stripe
        size="mini"
        :height="tableHeight"
      >
        <!-- 多选 -->
        <!-- <el-table-column type="selection" width="55" fixed></el-table-column> -->
        <el-table-column
          label="序号"
          type="index"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column
          label="角色名称"
          prop="nameZh"
        ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row, $index }">
            <span class="operate-btn" @click="lookClick(row)" type="primary" size="mini"
              >查看</span
            >
            <span v-loading.fullscreen.lock="fullscreenLoading" class="operate-btn" @click="onEditRole(row)" type="primary" size="mini"
              >编辑</span
            >
            <span class="operate-btn"
              type="primary"
              size="mini"
              @click="deleteMenu(row, $index)"
            >
              删除
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <!-- <span class="demonstration">完整功能</span> -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="paginationOptions.pageNo"
          :page-sizes="paginationOptions.pageSizes"
          :page-size="paginationOptions.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paginationOptions.total"
          size="mini"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
    <role-edit-dialog
      :toChild="list"
      ref="roleEditDialogRef"
    ></role-edit-dialog>
    <role-data-dialog
      :toChild="list"
      ref="roleDataDialogRef"
    ></role-data-dialog>
  </div>
</template>

<script>
import { queryRole, deleteRole } from "@/api/role";
import RoleEditDialog from "@/views/sysmanage/roles/dialog/roleEdit.vue";
import RoleDataDialog from "@/views/sysmanage/roles/dialog/roleDetails.vue";
export default {
  components: {
    RoleEditDialog,
    RoleDataDialog,
  },
  data() {
    return {
      fullscreenLoading: false,
      list: "",
      formOptions: {
        name: "",
        nameZh: "",
      },
      tableData: [],
      paginationOptions: {
        pageNo: 1,
        pageSizes: [10, 20, 30, 50, 100],
        pageSize: 10,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      // 验证
      rules: {},
    };
  },
  computed: {
    tableHeight() {
      return this.$store.state.tableHeight-440;
    }
  },
  mounted() {
    this.queryRoles();
  },
  methods: {
    // 手动 查询
    queryRolesclick() {
      this.$refs["queryRoleRef"].validate((valid) => {
        if (valid) {
          this.paginationOptions.pageNo = 1;
          this.queryRoles();
        } else {
          return false;
        }
      });
    },
    // 查询
    queryRoles() {
      this.$refs["queryRoleRef"].validate((valid) => {
        if (valid) {
          let data = { records: [{ ...this.formOptions }] };
          data.current = this.paginationOptions.pageNo;
          data.size = this.paginationOptions.pageSize;
          // console.log(data, "data---------");
          queryRole(data).then((res) => {
            // console.log(res, "res++++++++++");
            if (res && res.code && res.code === "00000") {
              // this.resetForm("queryRoleRef"); // 重置表单
              this.tableData = res.data.records; // 表格数据赋值
              this.paginationOptions.total = res.data.total; // 分页器赋值
            }
          });
        } else {
          return false;
        }
      });
    },
    // 删除弹框
    deleteMenu(row, index) {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "删除当前角色", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
        modal: false,
      })
        .then(() => {
          this.tableData.splice(index, 1);
          // 点击确认，发起后台请求，删除该用户
          deleteRole(row.id).then((res) => {
            console.log(res, "点击确认，发起后台请求，删除");
            if (res && res.code && res.code === "00000") {
              this.queryRoles();
              return this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          // 点击取消，取消该操作
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 添加
    addClick() {
      this.$refs.roleEditDialogRef.openDialog();
      this.list = "添加角色";
      console.log("我要添加");
    },
    // 查看
    lookClick(row) {
      this.$refs.roleDataDialogRef.openDialog(row);
      this.list = "查看角色详情";
      console.log("我要查看", row);
    },
    // 编辑
    async onEditRole(row) {
      const loading = this.$loading({
        lock: true,
        text: '拼命加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      await this.$refs.roleEditDialogRef.openDialog(row);
      this.list = "编辑角色信息";
      loading.close()
    },
    // 重置表单
    resetForm(formName) {
      console.log("重置-------", formName);
      this.$refs[formName].resetFields();
      this.queryRoles();
    },
    // 表格复选动作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页器 页容量变更行为
    handleSizeChange(val) {
      this.paginationOptions.pageSize = val;
      this.queryRoles();
    },
    // 分页器 页码变更行为
    handleCurrentChange(val) {
      this.paginationOptions.pageNo = val;
      this.queryRoles();
    },
    indexMethod(index) {
      return (
        (this.paginationOptions.pageNo - 1) * this.paginationOptions.pageSize +
        index +
        1
      );
    },
  },
};
</script>
<style lang="less" scoped>
::v-deep .el-col-19 {
  text-align: right;
}
.el-form--inline .el-form-item {
  margin-right: 0;
}
::v-deep .el-card__body {
  .el-form-item--mini.el-form-item {
    margin-bottom: 0;
  }
}
::v-deep .el-form-item__label {
  margin-right: 5px;
}
@media screen and (min-width: 800px) {
  ::v-deep .el-card__body::-webkit-scrollbar {
    display: none;
  }
}
::v-deep .el-card__body {
  .el-form-item--mini.el-form-item {
    margin-bottom: 0;
  }
}
</style>

